﻿// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace Microsoft.Data.Entity.Design.Model.Entity
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Xml.Linq;

    internal class UsingElement : EFNormalizableItem
    {
        internal static readonly string ElementName = "Using";
        internal static readonly string AttributeAlias = "Alias";
        internal static readonly string AttributeNamespace = "Namespace";

        private DefaultableValue<string> _aliasAttr;
        private DefaultableValue<string> _namespaceAttr;

        internal UsingElement(BaseEntityModel model, XElement element)
            : base(model, element)
        {
        }

        internal override string EFTypeName
        {
            get { return ElementName; }
        }

        /// <summary>
        ///     Returns the string value of the Alias attribute
        /// </summary>
        internal DefaultableValue<string> Alias
        {
            get
            {
                if (_aliasAttr == null)
                {
                    // we can safely create these here since we are the top node and don't need to be parsed first
                    _aliasAttr = new AliasDefaultableValue(this);
                }
                return _aliasAttr;
            }
        }

        private class AliasDefaultableValue : DefaultableValue<string>
        {
            internal AliasDefaultableValue(EFElement parent)
                : base(parent, AttributeAlias)
            {
            }

            internal override string AttributeName
            {
                get { return AttributeAlias; }
            }

            public override string DefaultValue
            {
                get { return String.Empty; }
            }
        }

        /// <summary>
        ///     Returns the string value of the Namespace attribute
        /// </summary>
        internal DefaultableValue<string> Namespace
        {
            get
            {
                if (_namespaceAttr == null)
                {
                    // we can safely create these here since we are the top node and don't need to be parsed first
                    _namespaceAttr = new NamespaceDefaultableValue(this);
                }
                return _namespaceAttr;
            }
        }

        private class NamespaceDefaultableValue : DefaultableValue<string>
        {
            internal NamespaceDefaultableValue(EFElement parent)
                : base(parent, AttributeNamespace)
            {
            }

            internal override string AttributeName
            {
                get { return AttributeNamespace; }
            }

            public override string DefaultValue
            {
                get { return String.Empty; }
            }
        }

        // we unfortunately get a warning from the compiler when we use the "base" keyword in "iterator" types generated by using the
        // "yield return" keyword.  By adding this method, I was able to get around this.  Unfortunately, I wasn't able to figure out
        // a way to implement this once and have derived classes share the implementation (since the "base" keyword is resolved at 
        // compile-time and not at runtime.
        private IEnumerable<EFObject> BaseChildren
        {
            get { return base.Children; }
        }

        internal override IEnumerable<EFObject> Children
        {
            get
            {
                foreach (var efobj in BaseChildren)
                {
                    yield return efobj;
                }

                yield return Alias;
                yield return Namespace;
            }
        }

#if DEBUG
        internal override ICollection<string> MyAttributeNames()
        {
            var s = base.MyAttributeNames();
            s.Add(AttributeAlias);
            s.Add(AttributeNamespace);
            return s;
        }
#endif

        protected override void PreParse()
        {
            Debug.Assert(State != EFElementState.Parsed, "this object should not already be in the parsed state");

            ClearEFObject(_aliasAttr);
            _aliasAttr = null;
            ClearEFObject(_namespaceAttr);
            _namespaceAttr = null;

            base.PreParse();
        }

        protected override void DoNormalize()
        {
            NormalizedName = new Symbol(Namespace.Value);
            base.DoNormalize();
        }

        public override IValueProperty<string> Name
        {
            get { return Namespace; }
        }
    }
}
